/**
 * 
 */
package com.expense.listener;

import java.util.Timer;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;

import com.expense.util.ConfigUtil;
import com.expense.util.SqlConfig;

/**
 * /**
 * 定时任务调度监听
 * @author xj
 */
 
public class DbBackUpListener implements ServletContextListener {
	public Logger logger = Logger.getLogger(this.getClass());
	private Timer timer = null; 
	
	/* (non-Javadoc)
	 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent event) {
		// TODO Auto-generated method stub
		timer.cancel(); 
		//event.getServletContext().log("定时器销毁"); 
		logger.info("定时器销毁");
	}

	/* (non-Javadoc)
	 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent event) {
		// TODO Auto-generated method stub
		  timer = new Timer(true); 
		  //event.getServletContext().log("定时器已启动");
		  logger.info("定时器已启动");
		  
		  //timer.schedule(new MyTask(event.getServletContext()), 0, 60*60*1000); //每小时(即60*60*1000毫秒)被触发一次，中间参数0表示无延迟。 
		 
		  int cycle_SCHEDULE_HOUR=ConfigUtil.cycle_SCHEDULE_HOUR;//间隔周期小时数
		  if(cycle_SCHEDULE_HOUR==0){
			  cycle_SCHEDULE_HOUR=24;
		  }
		  timer.schedule(new DbBackUpTask(event.getServletContext()), 0, cycle_SCHEDULE_HOUR*60*60*1000); //每24小时(即24*60*60*1000毫秒)被触发一次，中间参数0表示无延迟。
		  //event.getServletContext().log("已经添加任务调度表"); 
		  logger.info("已经添加任务调度");
	}

}
